<template>
  <div class="goodsinfo-container">
    <!-- 小球 -->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="BallFlag" ref="ball"></div>
    </transition>
    <!-- 商品轮播图 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swipe :lunbotulist="lunbotu" :isfull="false"></swipe>
        </div>
      </div>
    </div>

    <!-- 商品购买区 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价:
            <del>¥{{ goodsinfo.market_price }}</del>&nbsp;&nbsp;
            销售价:
            <span class="now_price">¥ {{ goodsinfo.sell_price }}</span>
          </p>
          <p></p>
         <p>购买数量:<numbox @Getcount="getSelectedCount" :max="goodsinfo.stock_quantity"></numbox></p>
          <p>
            <van-button type="primary" size="small">立即购买</van-button>

            <van-button type="danger" size="small" @click="addToShopCar">加入购物车</van-button>
          </p>
        </div>
      </div>
    </div>

    <!-- 参数区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-header">商品参数</div>
        <div class="mui-card-content-inner">
          <p>商品货号: {{goodsinfo.goods_no}}</p>
          <p>库存情况: {{goodsinfo.stock_quantity}}</p>
          <p>上架时间: {{goodsinfo.add_time | dataFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <van-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</van-button>
        <van-button type="danger" size="large" plain @click="goComment(id)">商品评论</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import swipe from "../subcomponents/swipervue";
import numbox from "../subcomponents/Goodsinfo_numbox";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotu: [],
      goodsinfo: {},
      BallFlag: false,
      selectedCount:1
    };
  },
  created() {
    this.getLunbotu();
    this.getGoodInfo();
  },
  methods: {
    getLunbotu() {
      this.$ajax
        .request({
          url: "http://www.liulongbin.top:3005/api/getthumimages/" + this.id,
          method: "GET"
        })
        .then(ret => {
          if (ret.data.status === 0) {
            ret.data.message.forEach(item => {
              item.img = item.src;
            });
            this.lunbotu = ret.data.message;
          }
        });
    },
    getGoodInfo() {
      this.$ajax
        .request({
          url: "http://www.liulongbin.top:3005/api/goods/getinfo/" + this.id,
          method: "GET"
        })
        .then(ret => {
          if (ret.data.status === 0) {
            this.goodsinfo = ret.data.message[0];
          }
        });
    },
    goDesc(id) {
      this.$router.push({ name: "GoodsDesc", params: { id } });
    },
    goComment(id) {
      this.$router.push({ name: "GoodsComment", params: { id } });
    },
    addToShopCar() {
      // 添加到购物车
      this.BallFlag =! this.BallFlag
      // 拼接出要保存到 store 的商品信息
      var goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.getGoodInfo.sell_price,
        selected: true
      };
      this.$store.commit('AddToCart',goodsinfo)
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;
      //小球动画
      // 获取小球在页面中的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // 获取徽标位置
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      const xDist = badgePosition.left - ballPosition.left;
      const YDist = badgePosition.top - ballPosition.top;

      el.offsetWidth;
      el.style.transform = `translate(${xDist}px, ${YDist}px)`;
      el.style.transition = "all 0.6s cubic-bezier(1,1,.79,.95)";
      done();
    },
    afterEnter(el) {
      this.BallFlag =! this.BallFlag;
    },
    getSelectedCount(count) {
      // 当子组件把选中的数量传递给父组件的时候,把选中的值保存到data 上
      this.selectedCount = count;
    }
  },
  components: {
    swipe,
    numbox
  }
};
</script>

<style scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
  margin: 46px 0 50px 0;
}
.now_price {
  color: red;
  font-size: 16px;
  font-weight: bold;
}
.mui-card-footer {
  display: block;
}
.mui-card-footer button {
  margin: 15px 0;
}
.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: red;
  position: absolute;
  z-index: 99;
  top: 495px;
  left: 130px;
}
</style>